home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / img / Lib / imgxbm.py < prev   
Encoding:
Python Source  |  2000-06-23  |  2.2 KB  |  95 lines

  1. #
  2. # XBM image reader/writer module, python version
  3. #
  4. import string
  5. import imgformat
  6.  
  7. error = imgformat.error
  8.  
  9. class reader:
  10.     "Object that reads the image."
  11.     
  12.     def __init__(self, file):
  13.         """Initialize. You should read the header and fill in attributes
  14.         such as width, height, format_choices, format and colormap"""
  15.         
  16.         if type(file) == type(''):
  17.             self._filename = filename
  18.             self._fp = open(filename, 'rb')  # Yes, rb. Let's assume unix eolns...
  19.         else:
  20.             self._filename = '<open file>'
  21.             self._fp = file
  22.         self.width = self._getdefine()
  23.         self.height = self._getdefine()
  24.         self.format = imgformat.xbmpacked
  25.         self.format_choices = (self.format,)
  26.         
  27.     def _getdefine(self):
  28.         while 1:
  29.             line = self._fp.readline()
  30.             if not line:
  31.                 raise error, 'No #define found, not an XBM file?'
  32.             while line[-1] in ('\r', '\n'): line = line[:-1]
  33.             line = string.split(line)
  34.             if len(line) == 3 and line[0] == '#define':
  35.                 return string.atoi(line[2])
  36.  
  37.     def args(self):
  38.         return self.__dict__
  39.         
  40.     def read(self):
  41.         "Read the image data"
  42.         data = self._fp.read()
  43.         try:
  44.             bropen = string.index(data, '{')
  45.             brclose = string.index(data, '}')
  46.         except ValueError:
  47.             raise error, 'No braces found, not an XBM file?'
  48.         data = '(' + data[bropen+1:brclose-1] + ')'
  49.         try:
  50.             data = eval(data)
  51.         except ValueError:
  52.             raise error, 'Hexadecimal data not parseable, not an XBM file?'
  53.         rv = ''
  54.         for d in data:
  55.             # Note that xbm have white=0, black=1
  56.             rv = rv + chr(255-d)
  57.         return rv
  58.  
  59.     def write(self, data):
  60.         raise error, 'Cannot write() to reader'
  61.  
  62. class writer:
  63.     "Object that writes to an image file"
  64.     
  65.     def __init__(self, file):
  66.         if type(file) == type(''):
  67.             self._filename = filename
  68.             self._fp = None
  69.         else:
  70.             self._filename = '<open file>'
  71.             self._fp = file
  72.         self._filename = filename
  73.  
  74.     def args(self):
  75.         return self.__dict__
  76.         
  77.     def _get(self, attr):
  78.         try:
  79.             return getattr(self, attr)
  80.         except AttributeError:
  81.             raise error, "Required attribute '%s' missing"%attr
  82.  
  83.     def read(self):
  84.         raise error, 'Cannot read() from writer'
  85.  
  86.  
  87.     def write(self, data):
  88.         """Write the image file, according to attribute format"""
  89.         
  90.         w = self._get('width')
  91.         h = self._get('height')
  92.         if w*h != len(data):
  93.             raise error, 'Incorrect datasize'
  94.             
  95.